#!/usr/local/bin/python
#-*- encoding:utf-8 -*- 
import codecs 
from whoosh.index import create_in  
from whoosh.fields import *  
from chinesetokenizer import ChineseAnalyzer
#from whoosh.analysis import RegexAnalyzer  
#analyzer = RegexAnalyzer(ur"([\u4e00-\u9fa5])|(\w+(\.?\w+)*)")

from whoosh.index import open_dir

from whoosh.qparser import QueryParser


def cmp(t1, t2):
    k1, v1  = t1;
    k2, v2 = t2;
    return len(v2) - len(v1);

queryDict = {}
with open('finalunmatchLog', 'r') as f:
    for line in f:
        title, appID, query = line.strip().split('\t');
        if queryDict.has_key(query):
            if queryDict[query].has_key(title):
                queryDict[query][title] = queryDict[query][title] + 1;
            else:
                queryDict[query][title] = 1;
        else:
            queryDict[query] = {title:1};
li = sorted(queryDict.items(), cmp);
with open('finalunmatchLogS', 'w') as filter:
    for key ,value in li:

        filter.write('%s\t%s\n' %(key, '|'.join(['%s:%d'%(k,v) for k, v in sorted(value.items(), key=lambda x: x[1], reverse=True)])));

